package com.helehousekeeping.radiobuttonproject;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.helehousekeeping.radiobuttonproject.base.BaseActivity;
import com.helehousekeeping.radiobuttonproject.fragment.Fragmentfir;
import com.helehousekeeping.radiobuttonproject.fragment.Fragmentfour;
import com.helehousekeeping.radiobuttonproject.fragment.Fragmentone;
import com.helehousekeeping.radiobuttonproject.fragment.Fragmentthree;
import com.helehousekeeping.radiobuttonproject.fragment.Fragmenttwo;
import com.helehousekeeping.radiobuttonproject.fragment.MyFragment;
import com.helehousekeeping.radiobuttonproject.manager.ActivityCollector;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity {


    @Bind(R.id.rb_main)
    RadioGroup  mRbMain;
    @Bind(R.id.frameLayout_main)
    FrameLayout mFrameLayoutMain;
    private MyFragment          mMyFragment;
    private ArrayList<Fragment> mFragment;
    private Fragmentone         mFragmentone;
    private Fragmenttwo         mFragmenttwo;
    private Fragmentthree       mFragmentthree;
    private Fragmentfour        mFragmentfour;
    private Fragmentfir         mFragmentfir;
    private boolean mBackKeyPressed = false;

    @Override
    protected int setLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void setView() {
        initFragments();
        initRadioGroup();
        //默认为任务页
        mRbMain.check(0);
    }

    private void initRadioGroup() {
        for (int i = 0; i < mRbMain.getChildCount(); i++) {
            mRbMain.getChildAt(i).setId(i);
        }
        mRbMain.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                showFragment(i);
            }
        });
    }

    /**
     * 控制显示Fragment
     *
     * @param position
     */
    private void showFragment(int position) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

        for (int i = 0; i < mFragment.size(); i++) {
            if (i == position) {
                fragmentTransaction.show(mFragment.get(i));
            } else {
                fragmentTransaction.hide(mFragment.get(i));
            }
        }
        fragmentTransaction.commit();
    }

    private void initFragments() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (mFragmentone == null) {
            mFragmentone = new Fragmentone();
            fragmentTransaction.add(R.id.frameLayout_main, mFragmentone);
        }
        if (mFragmenttwo == null) {
            mFragmenttwo = new Fragmenttwo();
            fragmentTransaction.add(R.id.frameLayout_main, mFragmenttwo);
        }
        if (mFragmentthree == null) {
            mFragmentthree = new Fragmentthree();
            fragmentTransaction.add(R.id.frameLayout_main, mFragmentthree);
        }
        if (mFragmentfour == null) {
            mFragmentfour = new Fragmentfour();
            fragmentTransaction.add(R.id.frameLayout_main, mFragmentfour);
        }
        if (mFragmentfir == null) {
            mFragmentfir = new Fragmentfir();
            fragmentTransaction.add(R.id.frameLayout_main, mFragmentfir);
        }
        fragmentTransaction.commit();
        mFragment = new ArrayList();
        mFragment.add(mFragmentone);
        mFragment.add(mFragmenttwo);
        mFragment.add(mFragmentthree);
        mFragment.add(mFragmentfour);
        mFragment.add(mFragmentfir);

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    //通过线程延时的办法实现，借助Timer,实现双击退出程序
    @Override
    public void onBackPressed() {
        if (!mBackKeyPressed) {
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            mBackKeyPressed = true;
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    mBackKeyPressed = false;
                }
            }, 2000);
        } else {
            this.finish();
            ActivityCollector.finishAll();//双击退出结束所有的页面
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(this);
        ButterKnife.unbind(this);
    }
}
